/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import {
    Controller,
    Post,
    Body,
    UseGuards,
    UsePipes,
    Get
} from '@nestjs/common'
import { UserService } from './user.service'
import { AuthService } from '../auth/auth.service'
import { AuthGuard } from '@nestjs/passport'
import { ValidationPipe } from '../../pipe/validation.pipe'
import { ApiTags, ApiBearerAuth, ApiBody } from '@nestjs/swagger'
import { RegisterInfoDTO, LoginDTO, RegisterDTO } from './user.dto'
import { User } from './entity/user.entity'
@ApiBearerAuth() // Swagger 的 JWT 验证
@ApiTags('user')
@Controller('user')
export class UserController {
    constructor(
        private readonly authService: AuthService,
        private readonly usersService: UserService
    ) {}
    @Get('find-all')
    @ApiBody({
        description: '获取所有用户',
        type: RegisterDTO
    })
    findAll() {
        return this.usersService.findAll()
    }
    @Post('find-one')
    findOne(@Body() body: any) {
        return this.usersService.findOne(body.id)
    }
    // JWT验证 - Step 1: 用户请求登录
    @UseGuards(AuthGuard('local')) // 使用 'JWT' 进行验证
    @UsePipes(new ValidationPipe()) // 使用管道验证
    @ApiBody({
        description: '用户登录',
        type: LoginDTO
    })
    @Post('login')
    async login(@Body() loginParmas: LoginDTO) {
        console.log('JWT验证 - Step 1: 用户请求登录')
        const authResult = await this.authService.validateUser(
            loginParmas.username,
            loginParmas.password
        )
        switch (authResult.code) {
            case 1:
                return this.authService.certificate(authResult.user)
            case 2:
                return {
                    code: 600,
                    msg: `账号或密码不正确`
                }
            default:
                return {
                    code: 600,
                    msg: `查无此人`
                }
        }
    }
    @Post('register')
    @ApiBody({
        description: '用户注册',
        type: RegisterDTO
    })
    async register(@Body() body: RegisterInfoDTO) {
        return await this.usersService.register(body)
    }
    @Post()
    create(@Body() registerInfoDTO: RegisterInfoDTO): Promise<User> {
        return this.usersService.create(registerInfoDTO)
    }
}
